import { Application, FrontendSession, BackendSession } from 'pinus';
import orm_util from '../../../utils/orm_util';
import { User } from '../../../entity/user.entity';
import { Repository, FindOneOptions, FindManyOptions } from 'typeorm';
import net_result from '../../../consts/net_result';
import { UserService } from '../../../services/user_service';
import { Player } from '../../../entity/player.entity';
import { PlayerService } from '../../../services/player_service';
import { FightService } from '../../../services/fight_service';
import { Fight } from '../../../entity/fight';
import { PlayerDao } from '../../../dao/player_dao';
import { EffectType } from '../../../consts/effect_type';
import { Attack } from '../../../entity/effect/attack';
import { QualityType } from '../../../consts/quality_type';
import { GlobalConfig } from '../../../../config/global/global_config';
import { Treasure } from '../../../entity/treasure.entity';
import { TreasureDao } from '../../../dao/treasure_dao';
 
export default function (app: Application) {
    return new FightHandler(app);
}

 

export class FightHandler {

    constructor(private app: Application) {
     
    }
 
    async matchPlayer(msg, session: BackendSession) {
        // let enemy=await PlayerDao.ins().getRandomPlayer(msg.player.userId);
        let enemy=FightService.ins().getNextEnemy(msg.player);
        FightService.ins().addFight(new Fight(msg.player,enemy));
        return net_result.success(null,enemy);
    }

    async start(msg, session: BackendSession) {
        FightService.ins().getFight(msg.player.id).start();

    }

    async attackFinish(playerId){
        let effect=PlayerService.ins().getPlayer(playerId).getEffect(EffectType.Attack);
        if(!effect){
            (effect as Attack).setCanAttack(true);
        } 
    }
 
    async test(msg, session: BackendSession) {
    //     let treasures:Treasure[]=await(msg.player.treasures);
    //     let t:Treasure=null;
    //     let tR=await orm_util.getRepository(Treasure);
    //     let p=<Player>msg.player;
    //     t=(await p.treasures)[0];
    //     console.log("slice..before",await p.treasures);
    //     delete (await p.treasures)[0];
      
        
    //    await tR.remove(t);
    //    console.log("slice..after...",await p.treasures);
      
    }

    async buyTreasure(msg, session: BackendSession) {
       
        let treasures:Treasure[]=await(msg.player.treasures);
         console.log("length..",treasures.length)
        if((treasures.length)>Number(GlobalConfig.ins().game[0].maxTreasure)){
            return net_result.error(undefined,"已达"+GlobalConfig.ins().game[0].maxTreasure+"个宝箱上限");
        }
        if(msg.player.money>=GlobalConfig.ins().treasure[msg.qualityType].price){
            msg.player.money-=GlobalConfig.ins().treasure[msg.qualityType].price;
            return await PlayerDao.ins().updateForBuyTreasure(msg.player,msg.qualityType);
        }
        else{
            return net_result.error(undefined,"金币不足");
        }
     
    }

    async getTreasureConfig(msg, session: BackendSession) {
        return net_result.success(undefined,GlobalConfig.ins().treasure);
    }

    async getTreasure(msg, session: BackendSession) {
            let p:Player=msg.player;
            if(!msg.player.treasures){
                await orm_util.getRepository(Player);
            }
            let t=await p.treasures;
            return net_result.success(undefined,t);
 
    }

    async openTreasure(msg, session: BackendSession) {
        return await FightService.ins().openTreasure(msg);
    }


 
 

   

}